package entity

import (
	"fmt"
	"strings"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// ShJhcwJxffYjjx
type ShJhcwJxffYjjx struct {
	ID   int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	FFJD time.Time `gorm:"column:FFJD"`
	DPID int       `gorm:"column:DPID"`
	DPNM string    `gorm:"column:DPNM"`
	EMID int       `gorm:"column:EMID"`
	EMNO string    `gorm:"column:EMNO"`
	EMNM string    `gorm:"column:EMNM"`
	ZBID int       `gorm:"column:ZBID"`
	JXJE float64   `gorm:"column:JXJE"`
	DRID int       `gorm:"column:DRID"`
}

// TableName
func (ShJhcwJxffYjjx) TableName() string {
	return "SH_JHCW_JXFF_YJJX"
}

// BeforeSave
func (u *ShJhcwJxffYjjx) BeforeSave(tx *gorm.DB) (err error) {
	var emid, dpid, pdid *int
	var dpnm *string
	var emno, emnm string
	jdint := datenfjj(u.FFJD)
	err = can_import(tx, jdint)
	if err != nil {
		return
	}
	tx.Raw(`select "EMID","EMNM" from "SH_JHCW_JXFF_YGJX" where "FFJD"=? AND "EMNO" = ?`, jdint, u.EMNO).Row().Scan(&emid, &emnm)
	if emid == nil {
		err = tx.Raw(`select "DPID","DPNM","EMID","EMNO","EMNM","PDID" from "V_USER_DEPT" where "EMNO"=?`, u.EMNO).Row().Scan(&dpid, &dpnm, &emid, &emno, &emnm, &pdid)
		if err != nil || emid == nil {
			err = fmt.Errorf("员工号%s在类别绩效或绩效系统中不存在", u.EMNO)
			return
		}
		err = tx.Exec(`insert into "SH_JHCW_JXFF_YGJX" ("FFJD","DPID","DPNM","EMID","EMNO","EMNM","PDID") values(?,?,?,?,?,?,?)`, jdint, dpid, dpnm, emid, emno, emnm, pdid).Error
		if err != nil {
			return
		}
	}
	emnm_sys := strings.TrimSpace(emnm)
	emnm_imp := strings.TrimSpace(u.EMNM)
	if !strings.EqualFold(emnm_sys, emnm_imp) && !strings.Contains(emnm_sys, emnm_imp) {
		err = fmt.Errorf("员工号%s对应的姓名是%s,而提供姓名是%s", u.EMNO, emnm, u.EMNM)
		return
	}
	tx.Raw(`select "DPNM","DPID"  from "SH_JHCW_JXFF_YGJX" where "EMNO" = ? AND  "FFJD"=? `, u.EMNO, jdint).Row().Scan(&dpnm, &dpid)
	if dpid == nil {
		err = fmt.Errorf("部门%s在类别绩效中不存在,或者类别绩效没有导入", u.DPNM)
		return
	}
	u.DPID = *dpid
	u.DPNM = *dpnm
	u.EMID = *emid
	return
}

func ShJhcwJxffYjjxAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	ffjd := params["FFJD"]
	var date time.Time
	date, err = time.Parse("2006-01-02", ffjd)
	if err != nil {
		return
	}
	quarter := datenfjj(date)
	var sqlstr string
	sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" SET "YJJX" = 0 WHERE "FFJD" = ?`
	err = tx.Exec(sqlstr, quarter).Error
	if err != nil {
		return
	}
	if base.AppConf.Database.Mysql != "" {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A,(select "EMNO","EMID",sum("JXJE") "JXJE" from "SH_JHCW_JXFF_YJJX" where "FFJD"=? group by "EMNO","EMID") B
		set A."EMNO" = B."EMNO",A."EMID" = B."EMID",A."YJJX" = B."JXJE"
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	} else {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A
		set "EMNO" = B."EMNO","EMID" = B."EMID","YJJX" = B."JXJE"
		FROM (select "EMNO","EMID",sum("JXJE") "JXJE" from "SH_JHCW_JXFF_YJJX" where "FFJD"=? group by "EMNO","EMID") B
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	}
	err = tx.Exec(sqlstr, ffjd, quarter).Error
	if err != nil {
		return
	}
	if base.AppConf.Database.Mysql != "" {
		sqlstr = `UPDATE SH_JHCW_JXFF_YGJX A,(SELECT EMNO from SH_JHCW_JXFF_YJJX where ZBID = 7 AND FFJD = ?)B
		SET A.ISYYS =1
		WHERE A.EMNO = B.EMNO AND A.FFJD =?`
	} else {
		sqlstr = `update "SH_JHCW_JXFF_YGJX" A set "ISYYS" = 1
		from (select "EMNO" EMNO from "SH_JHCW_JXFF_YJJX" where "ZBID" = 7 and "FFJD" = ?)B
		where A."EMNO" = B.EMNO and A."FFJD" = ?`
	}
	err = tx.Exec(sqlstr, ffjd, quarter).Error
	if err != nil {
		return
	}
	return
}

func ShJhcwJxffYjjxEF() base.EntityFunc {
	return func() interface{} {
		entity := ShJhcwJxffYjjx{}
		return &entity
	}
}
func ShJhcwJxffYjjxESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShJhcwJxffYjjx
		return &entities
	}
}

// Jxffzb
type Jxffzb struct {
	ZBID int    `gorm:"column:ZBID"`
	ZBMC string `gorm:"column:ZBMC"`
}

// TableName
func (Jxffzb) TableName() string {
	return "SH_JHCW_JXFF_YJJX_ZB"
}

func JxffzbEF() base.EntityFunc {
	return func() interface{} {
		entity := Jxffzb{}
		return &entity
	}
}
func JxffzbESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Jxffzb
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("ShJhcwJxffYjjx", base.NewDefaultDao(ShJhcwJxffYjjxEF(), ShJhcwJxffYjjxESF(), nil, "my"))
	base.SetAfterCallBack("ShJhcwJxffYjjx", ShJhcwJxffYjjxAfterImport)
	base.DaoPoolObj.Put("Jxffzb", base.NewDefaultDao(JxffzbEF(), JxffzbESF(), nil, "my"))
}
